<?php
/**
 * 订单中心逻辑处理模块
 * User：LLW
 * Date: 2020-03-16
 */

namespace app\caidongweb\model;

use think\Db;
use think\Exception;

class OrderModel extends BaseModel
{
    protected $table = 'cd_order';

    /**
     * 订单列表
     * @param int $state 0:待付款 1:待发货 2:待收货 3:待评价 4:退货中 5:退货成功
     * @return \think\Paginator
     */
    public function getList($state = 0)
    {
        $where = [];
        $sta = strtotime(input('start'));
        $end = strtotime(input('end'));
        if($sta && $end){
            $where['or_xtime'] = array('between',"$sta,$end");
        }
        input('key') ? $where[input('city')]=input('key') : false;
        input('type') ? $where['or_type'] = input('type') : false;
        $list = $this->alias('o')
            ->join('cd_goods g','o.or_gid=g.go_id')
            ->join('cd_user u','o.or_uid=u.us_uid')
            ->where($where)
            ->where('or_state',$state)->field('o.*,g.*,u.us_name')
            ->order('or_xtime desc')
            ->paginate(30,false,['query'=>request()->param()]);
        return $list;
    }

    /**
     * 获取订单详情
     * @param $type
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getFind()
    {
        $orId = input('id');
        $list = $this->alias('o')
            ->join('cd_goods g','o.or_gid=g.go_id')
            ->join('cd_user u','o.or_uid=u.us_uid')
            ->where('or_id',$orId)
            ->field('o.*,g.*,u.us_name')
            ->find();
        $list['or_expr'] = unserialize($list['or_expr']);
        $list['or_add'] = unserialize($list['or_add']);
        return $list;
    }

    /**
     * 关闭待付款订单
     * @return array
     */
    public function getSingle()
    {
        $orId = input('id');
        $find = $this->where('or_id',$orId)->find();
        if(empty($find) || $find['or_state'] == 6){
            return $this->returnData(-1,'订单信息错误，请刷新重试');
        }
        $res = $this->where('or_id',$orId)->setField('or_state',6);
        if($res){
            accountUserLog($find['or_uid'],'订单:'.$find['or_batch'].'已被平台关闭');
            return $this->returnData(1,'成功');
        }else{
            return $this->returnData(-1,'哎呀，网络好像出问题了，请刷新重试');
        }
    }

    /**
     * 发货逻辑
     * @return array
     */
    public function getHair()
    {
        //订单信息
        $id = input('id');
        $find = $this->getFind();
        if($find['or_state'] != 1){
            return $this->returnData(-1,'该订单为无效订单或未付款订单');
        }
        //获取快递公司
        $expr = courier();
        //快递信息序列化
        $express = array(
            'ename' => $expr[trim(input('modules'))][0],
            'ecode' => $expr[trim(input('modules'))][1],
            'enumb' => trim(input('number')),
        );
        $data['or_state'] = 2;
        $data['or_expr'] = serialize($express);
        $data['or_ftime'] = time();
        $res = $this->where('or_id',$id)->update($data);
        if($res){
            accountUserLog($find['or_uid'],'订单：'.$find['or_batch'].'已发货，请注意查收');
            accountLog(2,1,session('sname'),'为订单编号:'.$find['or_batch'].'执行发货');
            return $this->returnData(1,'发货成功');
        }
        return $this->returnData(-1,'发货失败');
    }
}